package com.org.any.autocar.fragment;

import com.autocar.reader.R;
import com.autocar.reader.util.ConstantData;
import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.PopupWindow;
import android.widget.LinearLayout.LayoutParams;

public class ShareController {

	private PopupWindow sharePw;
	private LayoutInflater inflater;
	private View shareView;
	private IShareInfo ishareInfo;
	private Context context;

	public ShareController(Context context) {
		this.inflater = LayoutInflater.from(context);
		this.context = context;
		initview();
	}

	public void setIShareInfo(IShareInfo ishareInfo) {
		this.ishareInfo = ishareInfo;
	}

	private void initview() {
		if (shareView == null) {
			shareView = inflater
					.inflate(R.layout.html_content_menu_share, null);

			Button emailBtn = (Button) shareView
					.findViewById(R.id.html_menu_share_btn_email);
			Button sinaBtn = (Button) shareView
					.findViewById(R.id.html_menu_share_btn_sina);
			Button txBtn = (Button) shareView
					.findViewById(R.id.html_menu_share_btn_tx);
			Button noteBtn = (Button) shareView
					.findViewById(R.id.html_menu_share_btn_note);

			Button wxBtn = (Button) shareView
					.findViewById(R.id.html_menu_share_btn_wx);

			Button wxFDBtn = (Button) shareView
					.findViewById(R.id.html_menu_share_btn_wx_fd);
			Button backBtn = (Button) shareView
					.findViewById(R.id.html_menu_share_btn_cancel);

			MyOnclick on = new MyOnclick();

			wxBtn.setOnClickListener(on);
			wxFDBtn.setOnClickListener(on);
			emailBtn.setOnClickListener(on);
			noteBtn.setOnClickListener(on);
			backBtn.setOnClickListener(on);
			sinaBtn.setOnClickListener(on);
			txBtn.setOnClickListener(on);
		}
		if (sharePw == null) {
			sharePw = new PopupWindow(shareView, LayoutParams.FILL_PARENT,
					LayoutParams.WRAP_CONTENT, true);
			sharePw.setBackgroundDrawable(new BitmapDrawable());
		}
	}

	public void showPw(View view) {
		if (!sharePw.isShowing())
			sharePw.showAtLocation(view, Gravity.BOTTOM, 0, 0);
	}

	class MyOnclick implements OnClickListener {
		@Override
		public void onClick(View v) {
			switch (v.getId()) {
			case R.id.html_menu_share_btn_email:
				ishareInfo.sendEmail();
				break;
			case R.id.html_menu_share_btn_note:
				ishareInfo.sendNote();
				break;
			case R.id.html_menu_share_btn_sina:
				ishareInfo.sendSina();
				break;
			case R.id.html_menu_share_btn_tx:
				ishareInfo.sendTX();
				break;
			case R.id.html_menu_share_btn_wx:
				ishareInfo.sendWeixin(false);
				break;
			case R.id.html_menu_share_btn_wx_fd:
				ishareInfo.sendWeixin(true);
				break;
			}
			if (sharePw.isShowing()) {
				sharePw.dismiss();
			}
		}
	}

	public interface IShareInfo {
		public void sendEmail();

		public void sendNote();

		public void sendSina();

		public void sendTX();

		public void sendWeixin(boolean isfriend);
	}

}
